package com.itholmes.config;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import javax.servlet.ServletContext;

@Component
public class MyRunner implements ApplicationRunner {

    @Autowired
    ServletContext servletContext;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("项目启动~~~~");
        try {
            ZooKeeper zk = new ZooKeeper("150.158.199.52:2181", 10000, null);
            zk.create("/provider/consumer","127.0.0.1:8001".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
            //将zk对象放到servletContext里面让其一直存在，保持心跳检测。
            servletContext.setAttribute("zookeeper",zk);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
